package com.rtp.transformer.parser;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.jxpath.Container;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.xml.DocumentContainer;

import com.rtp.transformer.parser.exception.MappingNotExistedException;

 

/**
 * Husband factory configuration parser
 * @author xueming
 *
 */
public class Configuration {

	private String config;

	public Configuration(String config) {
		this.config = config;
	}
	
	public FormatFactory buildFactory() throws MappingNotExistedException{
		return new FormatFactoryDefaultImpl();
	}
	
	public static URL getResource(String resource) throws MappingNotExistedException {
		String stripped = resource.startsWith("/") ?
				resource.substring(1) : resource;

		URL url = null;
		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		if (classLoader!=null) {
			url = classLoader.getResource( stripped );
		}
		if ( url == null ) {
			url =  Configuration.class.getResource( resource );
		}
		if ( url == null ) {
			url =  Configuration.class.getClassLoader().getResource( stripped );
		}
		if ( url == null ) {
			throw new MappingNotExistedException(resource+" not found");
		}
		return url;
	}
	
	
	class FormatFactoryDefaultImpl implements FormatFactory{
		private final String XML_CONFIG = "format-configuration";
		private final String XML_FACTORY = "format-factory";
		private final String XML_PROPERTY = "property";
		private final String XML_MAPPING = "mapping";
		
		private Map parserMap = new HashMap();
		private String parserClass="com.rtp.transformer.parser.impl.TXTOutAndInStreamRtpImpl";
		
		
		public FormatFactoryDefaultImpl() throws MappingNotExistedException {
			
			URL url = getResource(config);
			
			Container locations = new DocumentContainer(url);
			JXPathContext con = JXPathContext.newContext(locations);
			parserClass =  getValue(con,
					XML_CONFIG+"/"+XML_FACTORY+"/"+XML_PROPERTY+"[@name='format.parser'])",
					parserClass);
			Double p = (Double) con.getValue("count("+XML_CONFIG+"/"+XML_FACTORY+"/"+XML_MAPPING+")");
			for(int i=1;i<p+1;i++){
				String resPath = XML_CONFIG+"/"+XML_FACTORY+"/"+XML_MAPPING+"[position()="+i+"]";
				String resource = getValue(con,resPath+"/@resource","");
				String key = getValue(con,resPath+"/@key","");
				TXTOutAndInStreamAdapter www = null;
				try {
					www = (TXTOutAndInStreamAdapter) Class.forName(parserClass).newInstance();
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				www.setResource(resource);
				parserMap.put(key,www);
			}
		}

		public TXTOutAndInStream getOutInStream(String parserConfig) {
			// TODO Auto-generated method stub
			return (TXTOutAndInStream) parserMap.get(parserConfig);
		}
		private String getValue(JXPathContext con,String xpath,String defaultVal){
			String value = defaultVal;
			try{
				value = (String) con.getValue(
						 xpath);
			}catch(Exception e){
			}
			return value;
		}

		 
	}
}
